package Thread.day01;
/**
 * 使用同步方法解决继承Thread类的线程安全问题
 */

public class Deemo6 {
    public static void main(String[] args) {
        window6 w1 = new window6();
        window6 w2 = new window6();
        window6 w3 = new window6();
        w1.setName("窗口1");
        w2.setName("窗口2");
        w3.setName("窗口3");
        w1.start();
        w2.start();
        w3.start();
    }


}
class window6 extends Thread{
    private  static int ticket = 100;
    @Override
    public void run() {
        while(true){
                sell();
            }

        }
        private static synchronized void sell(){
        //当前同步监视器默认：window6.class
            //但是同步方法必须是静态的，因为同步监视器必须只能是一个对象调用

            if (ticket>0){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"卖了票号为"+ticket);
                ticket--;
            }

    }
    }

